﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;

namespace QUAN_LY_MUON_TRA_SACH
{
    public partial class frmsuadg : Form
    {
        public frmsuadg()
        {
            InitializeComponent();
        }

        private void frmsuadg_Load(object sender, EventArgs e)
        {
            cbomathe.Focus();
            DataTable dbcbo_loaidg = new DataTable();
            SqlDataAdapter sqlDataAdapter = new SqlDataAdapter("select ma_loai_dgia,dgiai from loai_doc_gia", Connection.sqlConnection);
            sqlDataAdapter.Fill(dbcbo_loaidg);
            cboloaidg.DataSource = dbcbo_loaidg;
            cboloaidg.ValueMember = "ma_loai_dgia";

            sqlDataAdapter = new SqlDataAdapter("select ma_ng,ten_ng from nganh", Connection.sqlConnection);
            DataTable dbcbo_nganh = new DataTable();
            sqlDataAdapter.Fill(dbcbo_nganh);
            cbonganh.DataSource = dbcbo_nganh;
            cbonganh.ValueMember = "ma_ng";

            sqlDataAdapter = new SqlDataAdapter("select msnv,hten_nv from nhan_vien", Connection.sqlConnection);
            DataTable dbcbo_nhanvien = new DataTable();
            sqlDataAdapter.Fill(dbcbo_nhanvien);
            cbonhanvien.DataSource = dbcbo_nhanvien;
            cbonhanvien.ValueMember = "msnv";

            DataTable bang = new DataTable();
            sqlDataAdapter = new SqlDataAdapter("select mathe,hten_dg,ttrangthe from the_doc_gia", Connection.sqlConnection);
            sqlDataAdapter.Fill(bang);
            cbomathe.DataSource = bang;
            cbomathe.ValueMember = "mathe";

            txthoten.Enabled = false;
            cadngaysinh.Enabled = false;
            txtcmnd.Enabled = false;
            cbogioitinh.Enabled = false;
            cboloaidg.Enabled = false;
            cbonganh.Enabled = false;
            txtdiachi.Enabled = false;
            txtdienthoai.Enabled = false;
            txtemail.Enabled = false;
            cbotrang.Enabled = false;
            cadngaycap.Enabled = false;
            cadnghethan.Enabled = false;
            cbonhanvien.Enabled = false;
            //
            foreach (DataGridViewRow row in FORM_MAIN.frmmain.dataGridView1.Rows)
            {
                if (row.Cells[0].Value != null)
                {
                    cbomathe.Text = row.Cells[1].Value.ToString();
                }
            }
            
        }

        private void btnluu_Click(object sender, EventArgs e)
        {
            try
            {
                if (cbomathe.Text != "")
                {
                    SqlCommand lenh = new SqlCommand("update the_doc_gia set hten_dg=N'" + txthoten.Text + "',ngsinh_dg='" + cadngaysinh.Text + "',cmnd='" + txtcmnd.Text + "',gioitinh=N'" + cbogioitinh.Text + "',ma_loai_dgia='" + cboloaidg.Text + "',ma_ng='" + cbonganh.Text + "',diachi=N'" + txtdiachi.Text + "',dienthoai='" + txtdienthoai.Text + "',email='" + txtemail.Text + "',ttrangthe=N'" + cbotrang.Text + "',ngaycap='" + cadngaycap.Text + "',ngayhethan='" + cadnghethan.Text + "',msnv='" + cbonhanvien.Text + "',path_anh_doc_gia='" + openanhdocgia.FileName + "' where mathe='" + cbomathe.Text + "'", Connection.sqlConnection);
                    int n = lenh.ExecuteNonQuery();
                    MessageBox.Show("Đoc giả " + cbomathe.Text + " đã được cập nhật!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    btninthe.Enabled = true;
                }
                else
                    MessageBox.Show("Bạn chưa chọn đọc giả để sửa!", "Thông báo", MessageBoxButtons.OK, MessageBoxIcon.Error);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Lỗi: " + ex.Message);
            }
        }

        private void btnthoat_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        private void cbomathe_ValueChanged(object sender, EventArgs e)
        {
            ptbanhdg.Image = null;
            SqlCommand lenh = new SqlCommand("select mathe,hten_dg,ngsinh_dg,cmnd,gioitinh,ma_loai_dgia,ma_ng,diachi,dienthoai,email,ttrangthe,ngaycap,ngayhethan,msnv,path_anh_doc_gia from the_doc_gia where mathe='" + cbomathe.Text + "'", Connection.sqlConnection);
            SqlDataReader sqldatareader = lenh.ExecuteReader();
            while (sqldatareader.Read())
            {
                if (cbomathe.Text == sqldatareader.GetString(0))
                {
                    txthoten.Text = sqldatareader.GetString(1);
                    cadngaysinh.Text = sqldatareader.GetDateTime(2).ToString();
                    txtcmnd.Text = sqldatareader.GetString(3);
                    cbogioitinh.Text = sqldatareader.GetString(4);
                    cboloaidg.Text = sqldatareader.GetString(5);
                    cbonganh.Text = sqldatareader.GetString(6);
                    txtdiachi.Text = sqldatareader.GetString(7);
                    txtdienthoai.Text = sqldatareader.GetString(8);
                    txtemail.Text = sqldatareader.GetString(9);
                    cbotrang.Text = sqldatareader.GetString(10);
                    cadngaycap.Text = sqldatareader.GetDateTime(11).ToString();
                    cadnghethan.Text = sqldatareader.GetDateTime(12).ToString();
                    cbonhanvien.Text = sqldatareader.GetString(13);
                    //khoi tao gia tri hop anh doc gia
                    openanhdocgia.FileName = sqldatareader.GetString(14);
                    if (sqldatareader.GetString(14)!="")
                        ptbanhdg.Image = Image.FromFile(sqldatareader.GetString(14));
                    //
                    txthoten.Enabled = true;
                    cadngaysinh.Enabled = true;
                    txtcmnd.Enabled = true;
                    cbogioitinh.Enabled = true;
                    cboloaidg.Enabled = true;
                    cbonganh.Enabled = true;
                    txtdiachi.Enabled = true;
                    txtdienthoai.Enabled = true;
                    txtemail.Enabled = true;
                    cbotrang.Enabled = true;
                    cadngaycap.Enabled = true;
                    cadnghethan.Enabled = true;
                    cbonhanvien.Enabled = true;
                }
            }
            sqldatareader.Dispose();
        }

        private void btninthe_Click(object sender, EventArgs e)
        {
            rpt_the_doc_gia.mathe = cbomathe.Text;
            frmthe_doc_gia thedg = new frmthe_doc_gia();
            thedg.ShowDialog();
            btninthe.Enabled = false;
        }

        private void btnnapanh_Click(object sender, EventArgs e)
        {
            openanhdocgia.Title = "Mở File Sao Lưu";
            openanhdocgia.Filter = "Image Files(*.png)|*.png|(*.jpg)|*.jpg|All Files(*.*)|*.*";
            openanhdocgia.FileName = "";
            if (openanhdocgia.ShowDialog() == DialogResult.OK && openanhdocgia.FileName != "")
            {
                ptbanhdg.Image = Image.FromFile(openanhdocgia.FileName);
                
            }
           
        }
    }
}
